package com.app.qbake.module.square;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.wind.base.mvp.view.BaseLoadingFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ActivityUtil;
import com.wind.data.mine.AddressRequest;
import com.wind.data.mine.AddressResponse;
import com.wind.data.mine.bean.Address;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.EditAddressActivity;
import com.app.qbake.module.mine.di.AddressComponent;
import com.app.qbake.module.mine.event.EditAddressSuccessEvent;
import com.app.qbake.module.mine.mvp.AddressView;
import com.app.qbake.module.mine.presenter.AddressPresenter;
import com.app.qbake.module.square.adapter.SelectAddressAdapter;
import com.app.qbake.module.square.event.AddressItemClickEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectAddressFragment extends BaseLoadingFragment<AddressView,AddressPresenter,AddressComponent,AddressResponse>
        implements AddressView{

    private static final String ARGS_KEY_ADDR = "args_key_addr";

    @Inject
    AddressPresenter presenter;

    @BindView(R.id.rv_address)
    RecyclerView rv_address;
    SelectAddressAdapter mAdapter;
    Address mArgAddress;
    @Override
    public AddressPresenter createPresenter() {
        return presenter;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        mArgAddress= (Address) getArguments().getSerializable(ARGS_KEY_ADDR);
        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_address.setLayoutManager(manager);

        mAdapter=new SelectAddressAdapter(getActivity(),R.layout.item_select_address);
        rv_address.setAdapter(mAdapter);
    }

    @Override
    protected BaseRequest buildLoadingRequest() {
        AddressRequest request=new AddressRequest();
        request.setPage(1);
        request.setLimit(100);
        return request;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_address;
    }

    @Override
    public void showPage(AddressResponse reponse) {
        List<Address> addressList=reponse.getData();
        if (mArgAddress!=null){
            for (Address address:addressList){
                if (address.getId().equals(mArgAddress.getId())){
                    address.setSelected(true);
                    break;
                }
            }
        }
        mAdapter.replace(reponse.getData());
        mLayoutManager.showContent();
    }


    @OnClick({R.id.tv_add_address})
    public void onViewClick(View view){
        switch (view.getId()) {
            case R.id.tv_add_address:
                EditAddressActivity.start(getActivity());
                break;
        }
    }


    @Subscribe
    public void onEditAddressSuccessEvent(EditAddressSuccessEvent event){
        load();
    }

    @Subscribe
    public void onAddressItemClickEvent(AddressItemClickEvent event){
        ActivityUtil.finish(getActivity());
    }

    public static Fragment getInstance(Address address) {
        SelectAddressFragment f=new SelectAddressFragment();
        Bundle args=new Bundle();
        args.putSerializable(ARGS_KEY_ADDR,address);
        f.setArguments(args);
        return f;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
